﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiSha.Data;
using YiSha.Data.Repository;
using YiSha.Entity.SystemManage;
using YiSha.Enums;
using YiSha.Model;
using YiSha.Model.Param.SystemManage;
using YiSha.Service.SystemManage;
using YiSha.Util;
using YiSha.Util.Model;

namespace YiSha.Service.SystemManage
{
    public class LogLoginService : RepositoryFactory
    {

        #region 获取数据
        public async Task<TData<List<LogLoginEntity>>> GetList(LogLoginListParam param)
        {
            var strSql = new StringBuilder();
            var obj = new TData<List<LogLoginEntity>>();
            var filter = ListFilter(param, strSql);
            var list = SqlSugarAdapters.GetList<LogLoginEntity>(strSql.ToString(), filter);
            obj.Data = list;
            return obj;
        }

        public async Task<TData<List<LogLoginEntity>>> GetPageList(LogLoginListParam param, Pagination pagination)
        {
            var obj = new TData<List<LogLoginEntity>>();
            var strSql = new StringBuilder();
            var filter = ListFilter(param, strSql);
            var list = SqlSugarAdapters.GetPager<LogLoginEntity>(strSql.ToString(), filter, pagination);
            obj.Total = list.Item1;
            obj.Data = list.Item2;
            return obj;
        }

        public async Task<LogLoginEntity> GetEntity(string id)
        {
            return SqlSugarAdapters.GetEntity<LogLoginEntity>(s => s.Id == id);
        }
        #endregion

        #region 提交数据
        /// <summary>
        /// 保存数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<TData> SaveForm(LogLoginEntity entity)
        {
            var obj = new TData();
            if (entity.Id.IsEmpty())
            {
                await entity.Create();
                SqlSugarAdapters.Insert(entity);
            }
            else
            {
                SqlSugarAdapters.Update(entity);
            }
            return obj;
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<TData> DeleteForm(List<string> ids)
        {
            var obj = new TData();
            try
            {
                SqlSugarAdapters.Delete<LogLoginEntity>(d => ids.Contains(d.Id));
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }
        /// <summary>
        /// 清空所有数据
        /// </summary>
        /// <returns></returns>
        public async Task<TData> RemoveAllForm()
        {
            var obj = new TData();
            try
            {
                SqlSugarAdapters.ExecuteBySql("truncate table SysLogLogin");
            }
            catch (Exception ex)
            {
                obj.Msg = ex.Message;
                obj.MsgCode = MsgCode.Fail;
            }
            return obj;
        }
        #endregion

        #region 私有方法
        private Dictionary<string, object> ListFilter(LogLoginListParam param, StringBuilder strSql)
        {
            strSql.Append(@"SELECT  a.Id,
                                    a.BaseCreateTime,
                                    a.BaseCreatorId,
                                    a.LogStatus,
                                    a.IpAddress,
                                    a.IpLocation,
                                    a.Browser,
                                    a.OS,
                                    a.Remark,
                                    b.UserName
                            FROM    SysLogLogin a
                                    LEFT JOIN SysUser b ON a.BaseCreatorId = b.Id
                            WHERE   1 = 1");
            var parameter = new Dictionary<string, object>();
            if (param != null)
            {
                if (!string.IsNullOrEmpty(param.UserName))
                {
                    strSql.Append(" AND b.UserName like @UserName");
                    parameter.Add("UserName", '%' + param.UserName + '%');
                }
                if (param.LogStatus > -1)
                {
                    strSql.Append(" AND a.LogStatus = @LogStatus");
                    parameter.Add("LogStatus", param.LogStatus);
                }
                if (!string.IsNullOrEmpty(param.IpAddress))
                {
                    strSql.Append(" AND a.IpAddress like @IpAddress");
                    parameter.Add("IpAddress", '%' + param.IpAddress + '%');
                }
                if (!string.IsNullOrEmpty(param.StartTime.ParseToString()))
                {
                    strSql.Append(" AND a.BaseCreateTime >= @StartTime");
                    parameter.Add("StartTime", param.StartTime);
                }
                if (!string.IsNullOrEmpty(param.EndTime.ParseToString()))
                {
                    param.EndTime = param.EndTime.Value.Date.Add(new TimeSpan(23, 59, 59));
                    strSql.Append(" AND a.BaseCreateTime <= @EndTime");
                    parameter.Add("EndTime", param.EndTime);
                }
            }
            return parameter;
        }
        #endregion
    }
}
